package com.cinc.ebc.sd.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cinc.ebc.mm.entity.MmPurchaseRequisition;
import com.cinc.ebc.mm.entity.MmStockInOut;
import com.cinc.ebc.mm.entity.MmStockInOutDetail;
import com.cinc.ebc.mm.service.IMmStockInOutService;
import com.cinc.ebc.sd.entity.SdSalesOrder;
import com.cinc.ebc.sd.entity.SdSalesOrderDetail;
import com.cinc.ebc.sd.mapper.SdSalesOrderDetailMapper;
import com.cinc.ebc.sd.mapper.SdSalesOrderMapper;
import com.cinc.ebc.sd.model.SdSalesBackDetailModel;
import com.cinc.ebc.sd.model.SdSalesOrderDetailModel;
import com.cinc.ebc.sd.model.SdSalesOrderDetailOutHandleModel;
import com.cinc.ebc.sd.model.SdSalesOrderDetailOutModel;
import com.cinc.ebc.sd.service.ISdContractService;
import com.cinc.ebc.sd.service.ISdSalesOrderDetailService;
import com.cinc.ebc.sd.vo.SdSalesOrderDetailPage;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description: 销售订单明细
 * @Author: jeecg-boot
 * @Date:   2022-09-25
 * @Version: V1.0
 */
@Service
public class SdSalesOrderDetailServiceImpl extends ServiceImpl<SdSalesOrderDetailMapper, SdSalesOrderDetail> implements ISdSalesOrderDetailService {

	@Autowired
	private SdSalesOrderDetailMapper sdSalesOrderDetailMapper;

	@Autowired
	private IMmStockInOutService mmStockInOutService;

	@Autowired
	private SdSalesOrderMapper sdSalesOrderMapper;

	@Autowired
	private ISdContractService sdContractService;

	@Override
	public List<SdSalesOrderDetail> selectByMainId(String mainId) {
		return sdSalesOrderDetailMapper.selectByMainId(mainId);
	}

	@Override
	public IPage<SdSalesOrderDetailOutModel> outPage(Page<SdSalesOrderDetailOutModel> page, SdSalesOrderDetailPage detailPage) {
		String sql = QueryGenerator.installAuthJdbc(SdSalesOrder.class);
		return page.setRecords(sdSalesOrderDetailMapper.selectOutList(page,detailPage, sql));
	}

	@Override
	public List<SdSalesOrderDetailOutModel> outList(SdSalesOrderDetailPage detailPage) {
		return sdSalesOrderDetailMapper.selectOutList(null, detailPage,null);
	}

	@Override
	public IPage<SdSalesBackDetailModel> backPage(Page<SdSalesBackDetailModel> page, SdSalesOrderDetailPage detailPage) {

		return page.setRecords(sdSalesOrderDetailMapper.selectBackList(page,detailPage));
	}

	@Transactional
	@Override
	public void handleBack(List<SdSalesBackDetailModel> backDetailModelList) throws Exception {
		List<MmStockInOutDetail> stockInOutDetailList = new ArrayList<>();
		int stockInOutDetaiLineNo = 0;
		for(SdSalesBackDetailModel backDetailModel : backDetailModelList){
			// 出库单明细
			MmStockInOutDetail stockInOutDetail = new MmStockInOutDetail();
			stockInOutDetaiLineNo += 10;
			stockInOutDetail.setLineNo(stockInOutDetaiLineNo);
			stockInOutDetail.setMaterialId(backDetailModel.getMaterialId());
			stockInOutDetail.setMaterialNo(backDetailModel.getMaterialNo());
			stockInOutDetail.setMaterialDesc(backDetailModel.getMaterialDesc());
			stockInOutDetail.setUnit(backDetailModel.getUnit());
			stockInOutDetail.setQty(backDetailModel.getBackQty().negate());
			stockInOutDetail.setUnitPrice(backDetailModel.getUnitPrice());
			BigDecimal sumPrice = backDetailModel.getUnitPrice().multiply(stockInOutDetail.getQty()).setScale(2, BigDecimal.ROUND_HALF_UP);
			stockInOutDetail.setSumPrice(sumPrice);
			stockInOutDetail.setSourceId(backDetailModel.getSourceId());
			stockInOutDetail.setSourceNo(backDetailModel.getSourceNo());
			stockInOutDetail.setWarehouseId(backDetailModel.getWarehouseId());
			stockInOutDetail.setStorageLocationId(backDetailModel.getStorageLocationId());
			stockInOutDetail.setBatchNo(backDetailModel.getBatchNo());
			stockInOutDetailList.add(stockInOutDetail);
		}
		// 生成出库单
		MmStockInOut stockInOut = new MmStockInOut();
		stockInOut.setBillDate(new Date());
		stockInOut.setCustomerId(backDetailModelList.get(0).getCustomerId());
		mmStockInOutService.saveMainSalesOut(stockInOut, stockInOutDetailList, false);
	}

	@Transactional
	@Override
	public void handleOut(List<SdSalesOrderDetailOutHandleModel> outHandleModelList) throws Exception {
		List<MmStockInOutDetail> stockInOutDetailList = new ArrayList<>();
		List<String> orderNoList = outHandleModelList.stream().map(SdSalesOrderDetailOutHandleModel::getOrderNo).distinct().collect(Collectors.toList());
		List<String> contractIdList = new ArrayList<>();
		for(String orderNo : orderNoList){
			// 一个销单生成一个出库单
			int stockInOutLineNo = 0;
			String mainId = null;
			String customerId = null;
			String projectId = null;
			for(SdSalesOrderDetailOutHandleModel handleModel : outHandleModelList){
				if(!orderNo.equals(handleModel.getOrderNo())){
					continue;
				}
				BigDecimal outQty = handleModel.getOutQty();
				if(outQty == null || outQty.compareTo(BigDecimal.ZERO)==0){
					continue;
				}
				String contractId = handleModel.getContractId();
				if(oConvertUtils.isNotEmpty(contractId) && !contractIdList.contains(contractId)){
					contractIdList.add(contractId);
				}
				projectId = handleModel.getProjectId();
				customerId = handleModel.getCustomerId();
				// 更新数量、状态
				SdSalesOrderDetail orderDetail = sdSalesOrderDetailMapper.selectById(handleModel.getSalesOrderDetailId());
				SdSalesOrderDetail orderDetailModify = new SdSalesOrderDetail();
				orderDetailModify.setId(handleModel.getSalesOrderDetailId());
				orderDetailModify.setDeliveryQty(orderDetail.getDeliveryQty().add(handleModel.getOutQty()));
				sdSalesOrderDetailMapper.updateById(orderDetailModify);
				sdSalesOrderDetailMapper.autoUpdateStatus(handleModel.getSalesOrderDetailId());
				mainId = oConvertUtils.isEmpty(mainId) ? orderDetail.getMainId() : mainId;
				// 出库明细
				MmStockInOutDetail stockInOutDetail = new MmStockInOutDetail();
				stockInOutLineNo += 10;
				stockInOutDetail.setLineNo(stockInOutLineNo);
				stockInOutDetail.setMaterialId(handleModel.getMaterialId());
				stockInOutDetail.setMaterialNo(handleModel.getMaterialNo());
				stockInOutDetail.setMaterialDesc(handleModel.getMaterialDesc());
				stockInOutDetail.setUnit(handleModel.getUnit());
				stockInOutDetail.setQty(handleModel.getOutQty());
				stockInOutDetail.setUnitPrice(handleModel.getUnitPrice());
				BigDecimal sumPrice = handleModel.getUnitPrice().multiply(handleModel.getOutQty()).setScale(2, BigDecimal.ROUND_HALF_UP);
				stockInOutDetail.setSumPrice(sumPrice);
				stockInOutDetail.setSourceId(handleModel.getSalesOrderDetailId());
				stockInOutDetail.setSourceNo(handleModel.getOrderNo());
				stockInOutDetail.setWarehouseId(handleModel.getWarehouseId());
				stockInOutDetail.setStorageLocationId(handleModel.getStorageLocationId());
				stockInOutDetail.setBatchNo(handleModel.getBatchNo());
				stockInOutDetail.setProjectId(handleModel.getProjectId());
				stockInOutDetailList.add(stockInOutDetail);
			}
			// 生成出库单
			if(oConvertUtils.listIsNotEmpty(stockInOutDetailList)){
				MmStockInOut stockInOut = new MmStockInOut();
				stockInOut.setBillDate(new Date());
				stockInOut.setCustomerId(customerId);
				stockInOut.setProjectId(projectId);
				mmStockInOutService.saveMainSalesOut(stockInOut, stockInOutDetailList, false);
			}
			// 更新销单主表
			sdSalesOrderMapper.autoUpdateStatus(mainId);
		}
		// 更新销售合同
		if(oConvertUtils.listIsNotEmpty(contractIdList)){
			for(String contractId : contractIdList){
				sdContractService.autoUpdateContract(contractId);
			}
		}
	}

	@Override
	public IPage<SdSalesOrderDetailModel> detailPage(Page<SdSalesOrderDetailModel> page, SdSalesOrderDetailPage detailPage) {

		String sql = QueryGenerator.installAuthJdbc(SdSalesOrder.class);
		return page.setRecords(sdSalesOrderDetailMapper.selectDetailList(page, detailPage, sql));
	}

}
